package com.tcm.doctor.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.utils.R;
import com.tcm.prescribe.api.domain.dto.FrequentDTO;
import com.tcm.prescribe.api.domain.dto.FrequentItemDTO;
import com.tcm.prescribe.api.domain.vo.FrequentItemVO;
import com.tcm.prescribe.api.domain.vo.FrequentVO;
import com.tcm.prescribe.api.service.FrequentItemService;
import com.tcm.prescribe.api.service.FrequentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/doctor")
@RestController
public class FrequentController {

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private FrequentService frequentService;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private FrequentItemService frequentItemService;

    /**
     * 查询常用方名称
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/frequent/view")
    public R view(@RequestParam("id") Long id) {
        try {
            FrequentDTO frequentDTO = frequentService.findById(id);
            if(ObjectUtil.isEmpty(frequentDTO)){
                return R.success();
            }
            List<FrequentItemDTO> frequentItemDTOS = frequentItemService.listItemByFrequentId(id);
            List<FrequentItemVO> frequentItemVOS = DozerConvert.copyList(frequentItemDTOS,FrequentItemVO.class);
            FrequentVO frequentVO = new FrequentVO();
            BeanUtil.copyProperties(frequentDTO,frequentVO);
            frequentVO.setFrequentItemVoList(frequentItemVOS);
            return R.success(frequentVO);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail();
        }

    }

    /**
     * //保存常用方名称及药材
     * @param frequentDto
     * @return
     * @throws Exception
     */
    @PostMapping("/frequent/save")
    public R save(@RequestBody FrequentDTO frequentDto) throws ServiceException {
        try{
            frequentService.save(frequentDto);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 更新常用方名称
     * @param frequentDto
     * @return
     * @throws Exception
     */
    @PostMapping("/frequent/update")
    public R update(@RequestBody FrequentDTO frequentDto) throws ServiceException {
        try{
            frequentService.update(frequentDto);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 删除常用方
     * @param id
     * @return
     */
    @GetMapping("/frequent/delete")
    public R delete(@RequestParam("id") Long id) throws ServiceException{
        try{
            frequentService.deleteLogicById(id);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 分页查询常用方
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/frequent/list/page")
    public R listPageByUserId(@RequestParam("userId") Long userId, @RequestParam("page") Long page,
                           @RequestParam("pagesize") Long pagesize){
        PageBean listBean = frequentService.listPageByUserId(userId, page, pagesize);
        return R.success(listBean);
    }
}

